package jbit.service.impl;



import java.util.ArrayList;
import java.util.List;

import jbit.dao.ClaimVoucherDao;
import jbit.dao.ClaimVoucherDetailDao;
import jbit.entity.ClaimVoucher;
import jbit.entity.ClaimVoucherDetail;
import jbit.service.ClaimVoucherDetailService;

public class ClaimVoucherDetailServiceImpl implements ClaimVoucherDetailService {

	private ClaimVoucherDao claimVoucherDao;
	private ClaimVoucherDetailDao claimVoucherDetailDao;
	
	public ClaimVoucher addClaimVoucherDetail(ClaimVoucherDetail claimVoucherDetail) {
		ClaimVoucher claimVoucher=claimVoucherDetail.getBizClaimVoucher();
		if(claimVoucher.getId()==0 || null==claimVoucher.getId()){
			if(null==claimVoucher.getTotalAccount()||"".equals(claimVoucher.getTotalAccount())){
				claimVoucher.setTotalAccount(0d);
			}
			claimVoucher=this.claimVoucherDao.add(claimVoucher);
			claimVoucherDetail.setBizClaimVoucher(claimVoucher);
			this.claimVoucherDetailDao.add(claimVoucherDetail);
			claimVoucher.setTotalAccount(claimVoucherDetail.getAccount());
		}else{
			claimVoucher=this.claimVoucherDao.getClaimVoucher(claimVoucher);
			claimVoucherDetail.setBizClaimVoucher(claimVoucher);
			claimVoucherDetail=this.claimVoucherDetailDao.add(claimVoucherDetail);
			claimVoucher.setTotalAccount(claimVoucher.getTotalAccount()+claimVoucherDetail.getAccount());
		}
		claimVoucher=this.claimVoucherDao.update(claimVoucher);
		claimVoucher.setBizClaimVoucherDetails(this.getClaimVoucherDetailDao().getDetails(claimVoucher.getId()));
		return claimVoucher;

	}

	public ClaimVoucher modifyClaimVoucherDetail(ClaimVoucherDetail claimVoucherDetail) {

		ClaimVoucher claimVoucher=claimVoucherDetail.getBizClaimVoucher();
		claimVoucher=this.claimVoucherDao.getClaimVoucher(claimVoucher);
		ClaimVoucherDetail oldClaimVoucherDetail=null;
		Double acount=0d;
		List<ClaimVoucherDetail> list=new ArrayList<ClaimVoucherDetail>();
		if(list.size()>0){
			oldClaimVoucherDetail=list.get(0);
			acount=oldClaimVoucherDetail.getAccount();
		}
		oldClaimVoucherDetail.setAccount(claimVoucherDetail.getAccount());
		oldClaimVoucherDetail.setBizClaimVoucher(claimVoucherDetail.getBizClaimVoucher());
		oldClaimVoucherDetail.setDes(claimVoucherDetail.getDes());
		oldClaimVoucherDetail.setItem(claimVoucherDetail.getItem());
		this.claimVoucherDetailDao.update(oldClaimVoucherDetail);
		claimVoucher.setTotalAccount(claimVoucher.getTotalAccount()+(claimVoucherDetail.getAccount()-acount));
		claimVoucher=this.claimVoucherDao.update(claimVoucher);
		claimVoucher.setBizClaimVoucherDetails(this.getClaimVoucherDetailDao().getDetails(claimVoucher.getId()));
		return claimVoucher;

	}

	public void print() {


	}

	public ClaimVoucherDao getClaimVoucherDao() {
		return claimVoucherDao;
	}

	public void setClaimVoucherDao(ClaimVoucherDao claimVoucherDao) {
		this.claimVoucherDao = claimVoucherDao;
	}

	public ClaimVoucherDetailDao getClaimVoucherDetailDao() {
		return claimVoucherDetailDao;
	}

	public void setClaimVoucherDetailDao(ClaimVoucherDetailDao claimVoucherDetailDao) {
		this.claimVoucherDetailDao = claimVoucherDetailDao;
	}

}
